// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System;

namespace SnapObjects.Data
{
    internal class NoGenericModelFactory : NoGenericModelFactory<object>
    {
        public NoGenericModelFactory(IModelFactory<object> modelFactory) : base(modelFactory)
        {
        }

        public NoGenericModelFactory(Type modelType) : base(modelType)
        {
        }
    }

    internal class NoGenericModelFactory<TModel> : IModelFactory
    {
        private readonly IModelAccessor _accessor;
        private readonly IModelFactory<TModel> _factory;

        public NoGenericModelFactory(Type modelType)
            : this(new PocoModelFactory<TModel>(modelType))
        {
            _accessor = new NoGenericModelAccessor(this, modelType);
        }

        public NoGenericModelFactory(IModelFactory<TModel> modelFactory)
        {
            _factory = modelFactory;
        }

        public object Create()
        {
            return _factory.Create();
        }

        public IModelValidator<object> GetValidator()
        {
            return new NoGenericModelValidator();
        }

        public IModelAccessor GetAccessor()
        {
            return new NoGenericModelAccessor<TModel>(_factory.GetAccessor());
        }

        IModelAccessor<object> IModelFactory<object>.GetAccessor()
        {
            return this.GetAccessor();
        }
    }
}
